package com.ck.kafka;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.junit.Test;

import java.util.*;

/**
 * @Classname CustomConsumer
 * @Description 消费数据
 * @Date 2021/7/12 11:45
 * @Created by lpc
 */
public class CustomConsumer {

    /**
     * 自动提交offset
     */
    @Test
    public void testConsume(){
        Properties props = new Properties();
        props.put("bootstrap.servers","hadoop001:9092");
        props.put("group.id","test");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(props);
        consumer.subscribe(Arrays.asList("first"));
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records)
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
        }
    }

    /**
     * 手动提交offset-同步
     */
    @Test
    public void testHandSyncConsume(){
        Properties props = new Properties();
        props.put("bootstrap.servers","hadoop001:9092");
        props.put("group.id","test");
        props.put("enable.auto.commit", "false");
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(props);
        consumer.subscribe(Arrays.asList("first"));//消费者订阅主题
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records){
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
            }
            //同步提交，当前线程会阻塞直到 offset 提交成功
            consumer.commitSync();
        }

    }

    @Test
    public void testHandAsyncConsume(){
        Properties props = new Properties();
        props.put("bootstrap.servers","hadoop001:9092");
        props.put("group.id","test");
        props.put("enable.auto.commit", "false");
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(props);
        consumer.subscribe(Arrays.asList("first"));//消费者订阅主题
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records){
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
            }
            consumer.commitAsync(new OffsetCommitCallback() {
                public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception e) {
                    if (null != e){
                        System.err.println("Commit failed for" + offsets);
                    }
                }
            });
        }
    }

    public static Map<TopicPartition,Long> currentOffset = new HashMap<TopicPartition, Long>();





}
